package com.zhengsr.wanandroid.fragment;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.zhengsr.wanandroid.R;
import com.zhengsr.wanandroid.activity.CollectActivity;
import com.zhengsr.wanandroid.activity.LoginInActivity;
import com.zhengsr.wanandroid.mvp.base.BaseFragment;
import com.zhengsr.wanandroid.mvp.contract.ContractManager;
import com.zhengsr.wanandroid.mvp.present.LoginPresent;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by zhengshaorui
 * time: 2018/10/6
 */

public class UserFragment extends BaseFragment<LoginPresent> implements ContractManager.ILogoutView {
    private static final String TAG = "UserFragment";
    @BindView(R.id.user_login_tv)
    TextView mLoginText;

    @BindView(R.id.user_login_btn)
    Button mLoginoutBtn;
    

    private LoginPresent mLoginPresent;

    public static UserFragment newInstance(){
        return new UserFragment();
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_user;
    }

    @Override
    public LoginPresent getPresent() {
        mLoginPresent = LoginPresent.create(this);
        return mLoginPresent;
    }

    @Override
    protected boolean showNormalView() {
        return true;
    }

    @Override
    public void initView(View view) {

    }

    @Override
    public void onResume() {
        super.onResume();
        if (mLoginPresent.getLoginStatus()){
            mLoginText.setText(mLoginPresent.getLoginName());
            mLoginoutBtn.setVisibility(View.VISIBLE);
        }else{
            mLoginText.setText("登录/注册");
        }
    }

    @OnClick({R.id.user_login_tv,R.id.user_login_btn,R.id.user_start_ly})
    void onClickEvent(View view){
        switch (view.getId()){
            case R.id.user_login_tv:
                if (!mLoginPresent.getLoginStatus()) {
                    startActivity(new Intent(_mActivity, LoginInActivity.class));
                }
                break;
            case R.id.user_login_btn:
                if (mLoginPresent.getLoginStatus()) {
                    mLoginPresent.loginOut();
                }else{
                    Toast.makeText(_mActivity, "请先登录!", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.user_start_ly:
                if (mLoginPresent.getLoginStatus()) {
                    startActivity(new Intent(_mActivity, CollectActivity.class));
                }else{
                    Toast.makeText(_mActivity, "请先登录!", Toast.LENGTH_SHORT).show();
                }
                break;
            default:
                break;
        }

    }


    @Override
    public void showErrorMsg(String msg) {
        Log.d(TAG, "zsr --> showErrorMsg: "+msg);
    }


   /* @Override
    public void loginInSuccess(LoginBean bean) {
     //   Lgger.d("zsr --> sdfd: "+bean.toString());
        mLoginText.setText(mLoginPresent.getLoginName());
        Toast.makeText(_mActivity, "登录成功", Toast.LENGTH_SHORT).show();
        mLoginoutBtn.setVisibility(View.VISIBLE);
    }*/

    @Override
    public void loginOutSuccess() {
        mLoginText.setText("登录/注册");
        mLoginoutBtn.setVisibility(View.GONE);
        Toast.makeText(_mActivity, "已经退出登录", Toast.LENGTH_SHORT).show();
        
    }
}
